package com.caigou.modules.famous.ui.record

import android.os.Bundle
import android.text.TextUtils
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseActivity
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.famous.R
import com.caigou.modules.famous.adpter.MyRecordItemAdapter
import com.caigou.modules.famous.bean.MyRecordItemBean
import com.caigou.modules.famous.databinding.ActivityFamousMyRecordBinding
import com.caigou.modules.famous.viewmodel.MyRecordViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/11
 */
@Route(path = RouterPath.Famous.PAGE_MY_RECORD)
@AndroidEntryPoint
class FamousMyRecordActivity : BaseActivity() {

    private lateinit var binding: ActivityFamousMyRecordBinding

    private val myRecordViewModel: MyRecordViewModel by viewModels()

    private var famousType: Int = 1

    private val mAdapter: MyRecordItemAdapter by lazy {
        MyRecordItemAdapter(mutableListOf())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFamousMyRecordBinding.inflate(layoutInflater)
        setContentView(binding.root)
        containerId = binding.famousMyRecordLlContainer.id
        initView()
    }

    private fun initView() {
        ARouter.getInstance().inject(this)
        binding.famousMyRecordSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchMyRecordList()
            }
        }

        binding.famousMyRecordRecyclerView.run {
            layoutManager = LinearLayoutManager(this@FamousMyRecordActivity)
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(this@FamousMyRecordActivity))
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(RouterPath.Famous.PAGE_PERSONAL)
                .withInt(AppConstants.FAMOUS_TYPE, famousType)
                .withString(AppConstants.USER_ID, AppBackgroundInfo.instance.userBean!!.userId)
                .navigation()
        }

        binding.famousMyRecordRgSelector.setOnCheckedChangeListener { group, checkedId ->
            famousType = when (checkedId) {
                binding.famousMyRecordBtnHk.id -> 1
                binding.famousMyRecordBtnMacao.id -> 2
                else -> 1
            }
            fetchMyRecordList()
        }

        binding.famousMyRecordTvBack.setOnClickListener {
            finish()
        }

        fetchMyRecordList()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(myRecordViewModel.myRecordListLiveData, ::handleGetMyRecordList)
    }

    private fun handleGetMyRecordList(result: Resource<MutableList<MyRecordItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.famousMyRecordSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取我的战绩异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.famousMyRecordSwipeRefreshLayout.isRefreshing = false
                if (result.data == null) {
                    return
                }
                mAdapter.setList(result.data!!)
            }
        }
    }

    private fun fetchMyRecordList() {
        myRecordViewModel.getMyRecordList(famousType, AppBackgroundInfo.instance.userBean!!.userId)
    }
}